/*   
 * Copyright (c) 2016-2020 canaanQd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * canaanQd. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with canaanQd.   
 *   
 */ 

package com.lg.system.user.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lg.common.annotation.Logs;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.role.entity.Role;
import com.lg.system.role.service.RoleService;
import com.lg.system.user.entity.UserEntity;
import com.lg.system.user.service.UserRoleService;
import com.lg.system.user.service.UserService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController<UserEntity,UserService>{
	private String prefix="system/user/";
	@Autowired
	UserService userService;
	
	@Autowired
	RoleService roleService;
	
	@Autowired
	UserRoleService userRoleService;
	
	@RequiresPermissions("user:user")
	@GetMapping("index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("user:user")
	@Logs(value="登陆",module="用户模块")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody UserEntity userEntity){
		Query<UserEntity> query = new Query<UserEntity>(userEntity);
		List<UserEntity> users=userService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(users,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("user:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		mView.addObject("roles", selectRoles());
		return mView;
	}
	
	@RequiresPermissions("user:edit")
	@GetMapping("/edit/{userId}")
	public ModelAndView edit(@PathVariable("userId") Long userId){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		UserEntity user=userService.selectDetailById(userId);
		mView.addObject("obj",user );
		mView.addObject("roles", selectRoles());
		return mView;
	}
	
	private List<Role> selectRoles(){
		EntityWrapper<Role>  wrapper = new EntityWrapper<Role>();
		if(!getUserId().equals(1L)){
			wrapper.notIn("role_sign", "administrator");//非超级管理员添加用户选择角色时只能添加超级管理员以外的角色
		}
		List<Role> roles=roleService.selectList(wrapper);
		return roles;
	}
	
	@RequiresPermissions("user:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) UserEntity userEntity,BindingResult bindingResult){
		userEntity.setCreateId(getUserId());
		if(userService.save(userEntity)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("user:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) UserEntity userEntity,BindingResult bindingResult){
		userEntity.setModifiedId(getUserId());
		if(userService.update(userEntity)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("user:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) UserEntity userEntity,BindingResult bindingResult){
		if(userService.deleteById(userEntity.getUserId())){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("user:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(userService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
}
